﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;

    public class InboundHelper
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly IInvTransDA invTranDA = DataAccess.CreateInvTransDA();
        private static readonly ILocationDA locationDA = DataAccess.CreateLocationDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly IReceiptDetailLpnDA receiptDetailLpnDA = DataAccess.CreateReceiptDetailLpnDA();

        public void CheckLocationAttribute(DataBase database, DbTransaction tran, ReceiptDetailInfo receiptDetailInfo, string whLoginID)
        {
            ReceiptDetailBLL receiptDetailBll = new ReceiptDetailBLL();
            LocationInfo locationInfo = locationDA.GetLocationByID(database, tran, receiptDetailInfo.ToLoc, whLoginID);
            bool temp = true;
            if (locationInfo.IsMultiSku == "N")
            {
                DataSet ds = receiptDetailDA.GetSkuOnLoc(database, tran, receiptDetailInfo.ToLoc, whLoginID);
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    temp = receiptDetailBll.CheckIsMulitSkuCanPutAway2Loc(ds, receiptDetailInfo, temp);
                }
                else
                {
                    temp = true;
                }
            }
            if (!temp)
            {
                PublicMethod.GetInstance().ShowAlertMessage("F4200_001");
            }
            bool temp2 = true;
            if (locationInfo.IsMultiLot == "N")
            {
                DataSet ds2 = receiptDetailDA.GetLotOnLoc(database, tran, receiptDetailInfo.ToLoc, whLoginID);
                if ((ds2 != null) && (ds2.Tables[0].Rows.Count > 0))
                {
                    temp2 = receiptDetailBll.CheckIsMulitSkuCanPutAway2Loc(ds2, receiptDetailInfo, temp);
                }
                else
                {
                    temp2 = true;
                }
            }
            if (!temp2)
            {
                PublicMethod.GetInstance().ShowAlertMessage("F4200_002");
            }
        }

        public string CheckLotAttributeInfo(ReceiptLotInfo receiptLotInfo, string ownerID, string skuID, string whLoginID, string createdBy)
        {
            string lotID = string.Empty;
            ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
            lotID = lotAttributeDA.CheckIsHasSameLot(receiptLotInfo, ownerID, skuID, whLoginID);
            if (string.IsNullOrEmpty(lotID))
            {
                LotAttributeInfo lotInfo = new LotAttributeInfo {
                    Lot = commonMethod.GenNextNumStr("LOT_ATTRIBUTE", whLoginID),
                    LotAttr01 = receiptLotInfo.LotAttr01,
                    LotAttr02 = receiptLotInfo.LotAttr02,
                    LotAttr03 = receiptLotInfo.LotAttr03,
                    LotAttr04 = receiptLotInfo.LotAttr04,
                    LotAttr05 = receiptLotInfo.LotAttr05,
                    LotAttr06 = receiptLotInfo.LotAttr06,
                    LotAttr07 = receiptLotInfo.LotAttr07,
                    LotAttr08 = receiptLotInfo.LotAttr08,
                    LotAttr09 = receiptLotInfo.LotAttr09,
                    LotAttr10 = receiptLotInfo.LotAttr10,
                    OwnerID = ownerID,
                    SkuID = skuID,
                    ProduceDate = receiptLotInfo.ProduceDate,
                    ReceiptDate = receiptLotInfo.ReceiptDate,
                    ExpiryDate = receiptLotInfo.ExpiryDate,
                    ExternalLot = receiptLotInfo.ExternalLot,
                    VendorID = receiptLotInfo.VendorID,
                    CreatedBy = createdBy,
                    UpdatedBy = createdBy
                };
                if (lotAttributeDA.InsertLotAttribute(lotInfo, whLoginID) > 0)
                {
                    lotID = lotInfo.Lot;
                }
            }
            return lotID;
        }

        public string CheckLotAttributeInfo(DataBase database, DbTransaction tran, ReceiptLotInfo receiptLotInfo, string ownerID, string skuID, string whLoginID, string createdBy)
        {
            string lotID = string.Empty;
            ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
            lotID = lotAttributeDA.CheckIsHasSameLot(database, tran, receiptLotInfo, ownerID, skuID, whLoginID);
            if (string.IsNullOrEmpty(lotID))
            {
                LotAttributeInfo lotInfo = new LotAttributeInfo {
                    Lot = commonMethod.GenNextNumStr(database, tran, "LOT_ATTRIBUTE", whLoginID),
                    LotAttr01 = receiptLotInfo.LotAttr01,
                    LotAttr02 = receiptLotInfo.LotAttr02,
                    LotAttr03 = receiptLotInfo.LotAttr03,
                    LotAttr04 = receiptLotInfo.LotAttr04,
                    LotAttr05 = receiptLotInfo.LotAttr05,
                    LotAttr06 = receiptLotInfo.LotAttr06,
                    LotAttr07 = receiptLotInfo.LotAttr07,
                    LotAttr08 = receiptLotInfo.LotAttr08,
                    LotAttr09 = receiptLotInfo.LotAttr09,
                    LotAttr10 = receiptLotInfo.LotAttr10,
                    OwnerID = ownerID,
                    SkuID = skuID,
                    ProduceDate = receiptLotInfo.ProduceDate,
                    ReceiptDate = receiptLotInfo.ReceiptDate,
                    ExpiryDate = receiptLotInfo.ExpiryDate,
                    ExternalLot = receiptLotInfo.ExternalLot,
                    VendorID = receiptLotInfo.VendorID,
                    CreatedBy = createdBy,
                    UpdatedBy = createdBy
                };
                if (lotAttributeDA.InsertLotAttribute(database, tran, lotInfo, whLoginID) > 0)
                {
                    lotID = lotInfo.Lot;
                }
            }
            return lotID;
        }

        public string DeletePutawayTaskDetail(DataBase database, DbTransaction tran, string receiptID, string receiptLineID, string whLoginID)
        {
            return "";
        }

        public decimal ExchangeUom(PackInfo packInfo, string targetUom, decimal vqty, string flag)
        {
            float qty = Convert.ToSingle(vqty);
            float temp = 0f;
            if (qty == 0f)
            {
                temp = 0f;
            }
            else if (packInfo.PackUom1 == targetUom)
            {
                temp = qty;
            }
            else if (packInfo.PackUom2 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.InnerPack;
                }
                else
                {
                    temp = packInfo.InnerPack * qty;
                }
            }
            else if (packInfo.PackUom3 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.CaseCnt;
                }
                else
                {
                    temp = packInfo.CaseCnt * qty;
                }
            }
            else if (packInfo.PackUom4 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.Pallet;
                }
                else
                {
                    temp = packInfo.Pallet * qty;
                }
            }
            else if (packInfo.PackUom5 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.Cube;
                }
                else
                {
                    temp = packInfo.Cube * qty;
                }
            }
            else if (packInfo.PackUom6 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.GrossWgt;
                }
                else
                {
                    temp = packInfo.GrossWgt * qty;
                }
            }
            else if (packInfo.PackUom7 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.NetWgt;
                }
                else
                {
                    temp = packInfo.NetWgt * qty;
                }
            }
            else if (packInfo.PackUom8 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.OtherUnit1;
                }
                else
                {
                    temp = packInfo.OtherUnit1 * qty;
                }
            }
            else if (packInfo.PackUom9 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.OtherUnit2;
                }
                else
                {
                    temp = packInfo.OtherUnit2 * qty;
                }
            }
            return Convert.ToDecimal(temp);
        }

        public decimal ExchangeUom(string packID, string targetUom, decimal qty, string flag, string whLoginID)
        {
            PackInfo packInfo = packDA.GetPackByID(packID, whLoginID);
            return this.ExchangeUom(packInfo, targetUom, qty, flag);
        }

        public decimal ExchangeUom(DataBase dataBase, DbTransaction tran, string packID, string targetUom, decimal qty, string flag, string whLoginID)
        {
            PackInfo packInfo = DataAccess.CreatePackDA().GetPackByID(dataBase, tran, packID, whLoginID);
            return this.ExchangeUom(packInfo, targetUom, qty, flag);
        }

        public string GetASNDetailStatus(ReceiptDetailInfo receiptDetailInfo)
        {
            string status = string.Empty;
            if (receiptDetailInfo.ReceivedQty == 0M)
            {
                return "New";
            }
            if ((receiptDetailInfo.ReceivedQty != 0M) && (receiptDetailInfo.ReceivedQty < receiptDetailInfo.ExpectedQty))
            {
                return "InReceiving";
            }
            if (receiptDetailInfo.ReceivedQty >= receiptDetailInfo.ExpectedQty)
            {
                status = "ReceivedComplete";
            }
            return status;
        }

        public string GetASNStatus(List<ReceiptDetailInfo> receiptDetailInfos)
        {
            int flag = 0;
            int defailCount = 0;
            if ((receiptDetailInfos != null) && (receiptDetailInfos.Count > 0))
            {
                defailCount = receiptDetailInfos.Count;
                for (int i = 0; i < defailCount; i++)
                {
                    if (receiptDetailInfos[i].ReceivedQty == 0M)
                    {
                        flag = flag;
                    }
                    else if ((receiptDetailInfos[i].ReceivedQty != 0M) && (receiptDetailInfos[i].ReceivedQty < receiptDetailInfos[i].ExpectedQty))
                    {
                        flag++;
                    }
                    else if (receiptDetailInfos[i].ReceivedQty >= receiptDetailInfos[i].ExpectedQty)
                    {
                        flag += 2;
                    }
                }
            }
            if (flag == 0)
            {
                return "New";
            }
            if (flag == (defailCount * 2))
            {
                return "ReceivedComplete";
            }
            return "InReceiving";
        }

        public string GetASNStatus(DataSet ds)
        {
            decimal totalExpectedQty = decimal.Parse(ds.Tables[0].Rows[0]["TOTAL_EXPECTED_QTY"].ToString());
            decimal totalReceivedQty = decimal.Parse(ds.Tables[0].Rows[0]["TOTAL_RECEIVED_QTY"].ToString());
            string status = string.Empty;
            if (totalReceivedQty == 0M)
            {
                return "New";
            }
            if ((totalReceivedQty != 0M) && (totalReceivedQty < totalExpectedQty))
            {
                return "InReceiving";
            }
            if (totalReceivedQty >= totalExpectedQty)
            {
                status = "ReceivedComplete";
            }
            return status;
        }

        public string GetASNStatus(DataBase database, DbTransaction tran, ReceiptInfo receiptInfo, string whLoginID, PubParasInfo pubParasInfo)
        {
            string status = string.Empty;
            DataSet ds = null;
            if (receiptInfo != null)
            {
                if (receiptInfo.IsPalletized == "Y")
                {
                    ds = receiptDetailLpnDA.GetReceiptDetailTotalExpectedAndReceivedQty(database, tran, receiptInfo.ReceiptID, whLoginID);
                }
                else
                {
                    ds = receiptDetailDA.GetReceiptDetailTotalExpectedAndReceivedQty(database, tran, receiptInfo.ReceiptID, whLoginID);
                }
            }
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                status = this.GetASNStatus(ds);
            }
            return status;
        }

        public string GetPODetailStatus(PurchaseOrderDetailInfo purchaseOrderDetail)
        {
            string status = string.Empty;
            if (purchaseOrderDetail.ReceivedQty == 0M)
            {
                return "New";
            }
            if ((purchaseOrderDetail.ReceivedQty != 0M) && (purchaseOrderDetail.ReceivedQty < purchaseOrderDetail.OrderedQty))
            {
                return "InReceiving";
            }
            if (purchaseOrderDetail.ReceivedQty >= purchaseOrderDetail.OrderedQty)
            {
                status = "ReceivedComplete";
            }
            return status;
        }

        public string GetPurchaseOrderStatus(List<PurchaseOrderDetailInfo> purchaseOrderDetails)
        {
            int flag = 0;
            int defailCount = 0;
            if ((purchaseOrderDetails != null) && (purchaseOrderDetails.Count > 0))
            {
                defailCount = purchaseOrderDetails.Count;
                for (int i = 0; i < defailCount; i++)
                {
                    if (purchaseOrderDetails[i].ReceivedQty == 0M)
                    {
                        flag = flag;
                    }
                    else if ((purchaseOrderDetails[i].ReceivedQty != 0M) && (purchaseOrderDetails[i].ReceivedQty < purchaseOrderDetails[i].OrderedQty))
                    {
                        flag++;
                    }
                    else if (purchaseOrderDetails[i].ReceivedQty >= purchaseOrderDetails[i].OrderedQty)
                    {
                        flag += 2;
                    }
                }
            }
            if (flag == 0)
            {
                return "New";
            }
            if (flag == (defailCount * 2))
            {
                return "ReceivedComplete";
            }
            return "InReceiving";
        }

        public string GetPurchaseOrderStatus(string purchaseOrderID, string whLoginID)
        {
            return "";
        }

        public string GetPurchaseOrderStatus(DataBase database, DbTransaction tran, string purchaseOrderID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string status = string.Empty;
            List<PurchaseOrderDetailInfo> purchaseOrderDetails = DataAccess.CreatePurchaseOrderDetailDA().GetPurchaseOrderDetailListByID(database, tran, purchaseOrderID, whLoginID, pubParasInfo);
            if (purchaseOrderDetails != null)
            {
                status = this.GetPurchaseOrderStatus(purchaseOrderDetails);
            }
            return status;
        }

        public string InsertInvTrans(DataBase database, DbTransaction tran, ReceiptDetailInfo receiptDetailInfo, string transType, string transSourceType, string transStatus, string docID, int docLineID, string whLoginID)
        {
            string transID = commonMethod.GenNextNumStr(database, tran, "INV_TRANS", whLoginID);
            InvTransInfo invTransInfo = new InvTransInfo {
                WhID = whLoginID.Substring(0, whLoginID.Length - 1),
                TransID = transID,
                TransType = transType,
                SourceTransType = transSourceType,
                Status = transStatus,
                OwnerID = receiptDetailInfo.OwnerID,
                SkuID = receiptDetailInfo.SkuID,
                Lot = receiptDetailInfo.ToLot,
                FromLoc = receiptDetailInfo.FromLoc,
                FromLpnID = receiptDetailInfo.FromLpnID,
                ToLoc = receiptDetailInfo.ToLoc,
                ToLpnID = receiptDetailInfo.ToLpnID,
                DocID = docID,
                DocLineID = docLineID,
                UnitPrice = receiptDetailInfo.UnitPrice,
                LotAttr01 = receiptDetailInfo.ReceiptLot.LotAttr01,
                LotAttr02 = receiptDetailInfo.ReceiptLot.LotAttr02,
                LotAttr03 = receiptDetailInfo.ReceiptLot.LotAttr03,
                LotAttr04 = receiptDetailInfo.ReceiptLot.LotAttr04,
                LotAttr05 = receiptDetailInfo.ReceiptLot.LotAttr05,
                LotAttr06 = receiptDetailInfo.ReceiptLot.LotAttr06,
                LotAttr07 = receiptDetailInfo.ReceiptLot.LotAttr07,
                LotAttr08 = receiptDetailInfo.ReceiptLot.LotAttr08,
                LotAttr09 = receiptDetailInfo.ReceiptLot.LotAttr09,
                LotAttr10 = receiptDetailInfo.ReceiptLot.LotAttr10,
                ProduceDate = receiptDetailInfo.ReceiptLot.ProduceDate,
                ExpiryDate = receiptDetailInfo.ReceiptLot.ExpiryDate,
                ReceiptDate = receiptDetailInfo.ReceiptLot.ReceiptDate,
                ExternalLot = receiptDetailInfo.ReceiptLot.ExternalLot,
                VendorID = receiptDetailInfo.ReceiptLot.VendorID,
                CaseCnt = receiptDetailInfo.CaseCnt,
                InnerPack = receiptDetailInfo.InnerPack,
                Pallet = receiptDetailInfo.Pallet,
                Cube = receiptDetailInfo.Cube,
                GrossWgt = receiptDetailInfo.GrossWgt,
                NetWgt = receiptDetailInfo.NetWgt,
                OtherUnit1 = receiptDetailInfo.OtherUnit1,
                OtherUnit2 = receiptDetailInfo.OtherUnit2
            };
            PackInfo packInfo = packDA.GetPackByID(database, tran, receiptDetailInfo.PackID, whLoginID);
            invTransInfo.PackID = receiptDetailInfo.PackID;
            if (transSourceType == TransSourceType.ReceiptDetailAdd)
            {
                invTransInfo.UomCalc = 0;
                invTransInfo.UomID = receiptDetailInfo.UomID;
                invTransInfo.UomQty = receiptDetailInfo.ReceivedQty;
                invTransInfo.Qty = this.ExchangeUom(packInfo, receiptDetailInfo.UomID, receiptDetailInfo.ReceivedQty, "W");
                receiptDetailInfo.ReceivedQty = invTransInfo.Qty;
                invTransInfo.TransDate = receiptDetailInfo.ReceiptLot.ReceiptDate;
            }
            else if (transSourceType == TransSourceType.ReceiptReversal)
            {
                invTransInfo.UomCalc = 1;
                invTransInfo.UomID = packInfo.PackUom1;
                invTransInfo.UomQty = receiptDetailInfo.ReceivedQty;
                invTransInfo.Qty = receiptDetailInfo.ReceivedQty;
            }
            else
            {
                invTransInfo.UomCalc = 1;
                invTransInfo.UomID = packInfo.PackUom1;
                invTransInfo.UomQty = this.ExchangeUom(packInfo, receiptDetailInfo.UomID, receiptDetailInfo.ReceivedQty, "W");
                invTransInfo.Qty = invTransInfo.UomQty;
                receiptDetailInfo.ReceivedQty = invTransInfo.Qty;
            }
            invTransInfo.ReceiptID = receiptDetailInfo.ReceiptID;
            invTransInfo.ReceiptLineID = receiptDetailInfo.ReceiptLineID;
            invTransInfo.HoldCode = receiptDetailInfo.ConditionCode;
            invTransInfo.CreatedBy = receiptDetailInfo.UpdatedBy;
            invTransInfo.UpdatedBy = receiptDetailInfo.UpdatedBy;
            invTranDA.InsertInvTrans(database, tran, invTransInfo, whLoginID);
            return transID;
        }

        public string InsertPutawayTaskDetail(DataBase database, DbTransaction tran, ReceiptPutawayInfo receiptPutawayInfo, string taskType, string taskPriority, string whLoginID)
        {
            string taskDetailID = commonMethod.GenNextNumStr("TASK_DETAIL", whLoginID);
            TaskDetailInfo taskDetailInfo = new TaskDetailInfo {
                WhID = receiptPutawayInfo.WhID,
                TaskDetailID = taskDetailID,
                TaskType = taskType,
                OwnerID = receiptPutawayInfo.OwnerID,
                SkuID = receiptPutawayInfo.SkuID,
                Lot = receiptPutawayInfo.Lot,
                UomID = receiptPutawayInfo.Uom,
                UomQty = receiptPutawayInfo.Qty,
                Qty = receiptPutawayInfo.Qty,
                FromLoc = receiptPutawayInfo.FromLoc,
                LogicalFromLoc = "",
                FromLpnID = string.IsNullOrEmpty(receiptPutawayInfo.FromLpnID) ? "" : receiptPutawayInfo.FromLpnID,
                ToLoc = receiptPutawayInfo.ToLoc,
                LogicalToLoc = "",
                ToLpnID = receiptPutawayInfo.Lpn,
                CaseID = string.IsNullOrEmpty(receiptPutawayInfo.CaseID) ? "" : receiptPutawayInfo.CaseID,
                PickMethod = "",
                Status = TaskStatus.Pending,
                StatusMsg = "",
                Priority = taskPriority,
                SourcePriority = "",
                HoldID = receiptPutawayInfo.CondititonCode,
                UserID = "",
                UserPosition = "",
                UserIdOverride = "",
                StartTime = DateTime.Now,
                EndTime = DateTime.Now,
                SourceType = "",
                DocID = receiptPutawayInfo.ReceiptID,
                DocLineID = int.Parse(receiptPutawayInfo.ReceiptLineID),
                PickDetailID = "",
                OrderID = "",
                OrderLineID = 0,
                ListID = "",
                WaveID = "",
                ReasonID = "",
                Message01 = "",
                Message02 = "",
                Message03 = "",
                FinalToLoc = "",
                FromTransID = receiptPutawayInfo.TransID,
                CreatedBy = receiptPutawayInfo.UpdatedBy,
                UpdatedBy = receiptPutawayInfo.UpdatedBy
            };
            DataAccess.CreateTaskDetailDA().InsertTaskDetail(database, tran, taskDetailInfo, whLoginID);
            return taskDetailID;
        }

        public void UpdateInvInRevertReceiving(DataBase dataBase, DbTransaction tran, InvTransInfo receiptHistoryInfos, string whLoginID)
        {
            if (receiptHistoryInfos != null)
            {
                ReceiptDetailInfo tempReceiptDetail = new ReceiptDetailInfo {
                    OwnerID = receiptHistoryInfos.OwnerID,
                    SkuID = receiptHistoryInfos.SkuID,
                    ToLoc = receiptHistoryInfos.ToLoc,
                    ToLot = receiptHistoryInfos.Lot,
                    ToLpnID = receiptHistoryInfos.ToLpnID,
                    ReceivedQty = receiptHistoryInfos.Qty,
                    UpdatedBy = receiptHistoryInfos.UpdatedBy,
                    ReceiptID = receiptHistoryInfos.ReceiptID,
                    Cube = Convert.ToSingle(receiptHistoryInfos.Cube),
                    GrossWgt = Convert.ToSingle(receiptHistoryInfos.GrossWgt),
                    NetWgt = Convert.ToSingle(receiptHistoryInfos.NetWgt)
                };
                this.UpdateInvSkuLocQty(dataBase, tran, tempReceiptDetail, "subtract", whLoginID);
                this.UpdateInvLotQty(dataBase, tran, tempReceiptDetail, "subtract", whLoginID);
                this.UpdateLpn(dataBase, tran, tempReceiptDetail, "subtract", whLoginID);
                this.UpdateInvLotLocLpn(dataBase, tran, tempReceiptDetail, "subtract", whLoginID);
            }
        }

        public void UpdateInvLotLocLpn(DataBase database, DbTransaction tran, ReceiptDetailInfo receiptDetail, string updateType, string whLoginID)
        {
            IInvLotLocLpnDA invLotLocLpnDA = DataAccess.CreateInvLotLocLpnDA();
            InvLotLocLpnInfo invLotLocLpnInfo = invLotLocLpnDA.GetInvLotLocLpnByID(database, tran, receiptDetail.ToLot, receiptDetail.ToLoc, receiptDetail.ToLpnID, whLoginID);
            if ((invLotLocLpnInfo != null) && !string.IsNullOrEmpty(invLotLocLpnInfo.Loc))
            {
                if (updateType == "add")
                {
                    invLotLocLpnInfo.Qty += receiptDetail.ReceivedQty;
                }
                else if (updateType == "subtract")
                {
                    invLotLocLpnInfo.Qty -= receiptDetail.ReceivedQty;
                }
                invLotLocLpnInfo.UpdatedBy = receiptDetail.UpdatedBy;
                invLotLocLpnDA.UpdateInvLotLocLpn(database, tran, invLotLocLpnInfo, whLoginID);
            }
            else if (updateType == "subtract")
            {
                PublicMethod.GetInstance().ShowAlertMessage("F4200_003");
            }
            else
            {
                InvLotLocLpnInfo invLotLocLpn = new InvLotLocLpnInfo {
                    Lot = receiptDetail.ToLot,
                    Loc = receiptDetail.ToLoc,
                    LpnID = receiptDetail.ToLpnID,
                    OwnerID = receiptDetail.OwnerID,
                    SkuID = receiptDetail.SkuID,
                    Qty = receiptDetail.ReceivedQty,
                    Status = "OK",
                    ArchiveDate = DateTime.Parse("2000-01-01"),
                    CreatedBy = receiptDetail.UpdatedBy,
                    UpdatedBy = receiptDetail.UpdatedBy
                };
                invLotLocLpnDA.InsertInvLotLocLpn(database, tran, invLotLocLpn, whLoginID);
            }
        }

        public void UpdateInvLotQty(DataBase database, DbTransaction tran, ReceiptDetailInfo receiptDetail, string updateType, string whLoginID)
        {
            IInvLotDA invLotDA = DataAccess.CreateInvLotDA();
            InvLotInfo invLotInfo = invLotDA.GetInvLotByID(database, tran, receiptDetail.ToLot, whLoginID);
            if ((invLotInfo != null) && !string.IsNullOrEmpty(invLotInfo.Lot))
            {
                if (updateType == "add")
                {
                    invLotInfo.Qty += receiptDetail.ReceivedQty;
                }
                else if (updateType == "subtract")
                {
                    invLotInfo.Qty -= receiptDetail.ReceivedQty;
                }
                invLotInfo.CaseCnt = receiptDetail.CaseCnt;
                invLotInfo.InnerPack = receiptDetail.InnerPack;
                invLotInfo.Pallet = receiptDetail.Pallet;
                invLotInfo.Cube = receiptDetail.Cube;
                invLotInfo.GrossWgt = receiptDetail.GrossWgt;
                invLotInfo.NetWgt = receiptDetail.NetWgt;
                invLotInfo.OtherUnit1 = receiptDetail.OtherUnit1;
                invLotInfo.OtherUnit2 = receiptDetail.OtherUnit2;
                invLotInfo.UnitPrice = receiptDetail.UnitPrice;
                invLotInfo.UpdatedBy = receiptDetail.UpdatedBy;
                invLotDA.UpdateInvLot(database, tran, invLotInfo, whLoginID);
            }
            else if (updateType == "subtract")
            {
                PublicMethod.GetInstance().ShowAlertMessage("F4200_003");
            }
            else
            {
                InvLotInfo invLot = new InvLotInfo {
                    Lot = receiptDetail.ToLot,
                    OwnerID = receiptDetail.OwnerID,
                    SkuID = receiptDetail.SkuID,
                    CaseCnt = receiptDetail.CaseCnt,
                    InnerPack = receiptDetail.InnerPack,
                    Qty = receiptDetail.ReceivedQty,
                    Pallet = receiptDetail.Pallet,
                    Cube = receiptDetail.Cube,
                    GrossWgt = receiptDetail.GrossWgt,
                    NetWgt = receiptDetail.NetWgt,
                    OtherUnit1 = receiptDetail.OtherUnit1,
                    OtherUnit2 = receiptDetail.OtherUnit2,
                    UnitPrice = receiptDetail.UnitPrice,
                    Status = "OK",
                    ArchiveDate = DateTime.Parse("2000-01-01"),
                    CreatedBy = receiptDetail.UpdatedBy,
                    UpdatedBy = receiptDetail.UpdatedBy
                };
                invLotDA.InsertInvLot(database, tran, invLot, whLoginID);
            }
        }

        public void UpdateInvSkuLocQty(DataBase database, DbTransaction tran, ReceiptDetailInfo receiptDetail, string updateType, string whLoginID)
        {
            IInvSkuLocDA invSkuLocDA = DataAccess.CreateInvSkuLocDA();
            InvSkuLocInfo invSkuLocInfo = invSkuLocDA.GetInvSkuLocByID(database, tran, receiptDetail.OwnerID, receiptDetail.SkuID, receiptDetail.ToLoc, whLoginID);
            if (((invSkuLocInfo != null) && !string.IsNullOrEmpty(invSkuLocInfo.OwnerID)) && !string.IsNullOrEmpty(invSkuLocInfo.SkuID))
            {
                if (updateType == "add")
                {
                    invSkuLocInfo.Qty += receiptDetail.ReceivedQty;
                }
                else if (updateType == "subtract")
                {
                    invSkuLocInfo.Qty -= receiptDetail.ReceivedQty;
                }
                invSkuLocInfo.UpdatedBy = receiptDetail.UpdatedBy;
                invSkuLocDA.UpdateInvSkuLoc(database, tran, invSkuLocInfo, whLoginID);
            }
            else if (updateType == "subtract")
            {
                PublicMethod.GetInstance().ShowAlertMessage("F4200_003");
            }
            else
            {
                InvSkuLocInfo invSkuLoc = new InvSkuLocInfo {
                    OwnerID = receiptDetail.OwnerID,
                    SkuID = receiptDetail.SkuID,
                    Loc = receiptDetail.ToLoc,
                    LocationType = "",
                    Qty = receiptDetail.ReceivedQty,
                    CreatedBy = receiptDetail.UpdatedBy,
                    UpdatedBy = receiptDetail.UpdatedBy
                };
                invSkuLocDA.InsertInvSkuLoc(database, tran, invSkuLoc, whLoginID);
            }
        }

        public void UpdateLpn(DataBase database, DbTransaction tran, ReceiptDetailInfo receiptDetail, string updateType, string whLoginID)
        {
            ILpnDA lpnDA = DataAccess.CreateLpnDA();
            if (!string.IsNullOrEmpty(receiptDetail.ToLpnID))
            {
                LpnInfo lpnInfo = lpnDA.GetLpnByID(database, tran, receiptDetail.ToLpnID, whLoginID);
                if ((lpnInfo != null) && !string.IsNullOrEmpty(lpnInfo.LpnID))
                {
                    if (updateType == "add")
                    {
                        lpnInfo.Qty += receiptDetail.ReceivedQty;
                    }
                    else if (updateType == "subtract")
                    {
                        lpnInfo.Qty -= receiptDetail.ReceivedQty;
                    }
                    if (lpnInfo.DocType == "IN")
                    {
                        lpnInfo.DocID = receiptDetail.ReceiptID;
                    }
                    lpnInfo.UpdatedBy = receiptDetail.UpdatedBy;
                    lpnDA.UpdateLpn(database, tran, lpnInfo, whLoginID);
                }
                else
                {
                    LpnInfo lpn = new LpnInfo {
                        LpnID = receiptDetail.ToLpnID,
                        Qty = receiptDetail.ReceivedQty,
                        Status = "OK",
                        PackID = receiptDetail.PackID,
                        DocID = receiptDetail.ReceiptID,
                        DocType = LpnDocType.InBound,
                        CreatedBy = receiptDetail.UpdatedBy,
                        UpdatedBy = receiptDetail.UpdatedBy,
                        IsActive = "Y"
                    };
                    lpnDA.InsertLpn(database, tran, lpn, whLoginID);
                }
            }
        }

        public void UpdatePoQtyStatus(DataBase dataBase, DbTransaction tran, ReceiptDetailInfo recedetail, string updateType, string whLoginID, PubParasInfo pubParasInfo)
        {
            IPurchaseOrderDA purchaseOrderDA = DataAccess.CreatePurchaseOrderDA();
            IPurchaseOrderDetailDA purchaseOrderDetailDA = DataAccess.CreatePurchaseOrderDetailDA();
            PurchaseOrderInfo purchaseOrder = purchaseOrderDA.GetPurchaseOrderByID(dataBase, tran, recedetail.PoID, whLoginID, pubParasInfo);
            if (purchaseOrder != null)
            {
                purchaseOrder.PurchaseOrderDetailInfos = purchaseOrderDetailDA.GetPurchaseOrderDetailListByID(dataBase, tran, recedetail.PoID, whLoginID, pubParasInfo);
                if ((purchaseOrder.PurchaseOrderDetailInfos != null) && (purchaseOrder.PurchaseOrderDetailInfos.Count > 0))
                {
                    foreach (PurchaseOrderDetailInfo purchaseOrderDetail in purchaseOrder.PurchaseOrderDetailInfos)
                    {
                        if (purchaseOrderDetail.LineID == recedetail.PoLineID)
                        {
                            if (updateType == "add")
                            {
                                purchaseOrderDetail.ReceivedQty += recedetail.ReceivedQty;
                            }
                            else if (updateType == "subtract")
                            {
                                purchaseOrderDetail.ReceivedQty += recedetail.ReceivedQty;
                                if (purchaseOrderDetail.ReceivedQty < 0M)
                                {
                                    purchaseOrderDetail.ReceivedQty = 0M;
                                }
                            }
                            purchaseOrderDetail.Status = this.GetPODetailStatus(purchaseOrderDetail);
                            purchaseOrderDetail.UpdatedBy = recedetail.UpdatedBy;
                            purchaseOrderDetailDA.UpdatePurchaseOrderDetail(dataBase, tran, purchaseOrderDetail, new List<string> { whLoginID });
                        }
                    }
                    purchaseOrder.Status = this.GetPurchaseOrderStatus(purchaseOrder.PurchaseOrderDetailInfos);
                    purchaseOrder.UpdatedBy = recedetail.UpdatedBy;
                    purchaseOrderDA.UpdatePurchaseOrder(dataBase, tran, purchaseOrder, new List<string> { whLoginID });
                }
            }
        }

        public void UpdateReceiptDetailQtyStatus(DataBase dataBase, DbTransaction tran, ReceiptInfo receiptInfo, ReceiptDetailInfo receiptDetailInfo, string updateType, string whLoginID, PubParasInfo pubParasInfo)
        {
            ReceiptDetailInfo originReceiptDetail = null;
            if (receiptInfo.IsPalletized == "Y")
            {
                originReceiptDetail = receiptDetailLpnDA.GetReceiptDetailLpnByID(dataBase, tran, receiptDetailInfo.ReceiptID, receiptDetailInfo.ReceiptLineID.ToString(), whLoginID, pubParasInfo);
            }
            else
            {
                originReceiptDetail = receiptDetailDA.GetReceiptDetailByID(dataBase, tran, receiptDetailInfo.ReceiptID, receiptDetailInfo.ReceiptLineID.ToString(), whLoginID, pubParasInfo);
            }
            if (updateType == "add")
            {
                originReceiptDetail.ReceivedQty += receiptDetailInfo.ReceivedQty;
            }
            else if (updateType == "")
            {
                originReceiptDetail.ReceivedQty -= receiptDetailInfo.ReceivedQty;
            }
            originReceiptDetail.ReceivedDate = receiptDetailInfo.ReceivedDate;
            originReceiptDetail.ReceiptLot.ReceiptDate = receiptDetailInfo.ReceiptLot.ReceiptDate;
            originReceiptDetail.Status = this.GetASNDetailStatus(originReceiptDetail);
            originReceiptDetail.ToLoc = receiptDetailInfo.ToLoc;
            originReceiptDetail.ToLot = receiptDetailInfo.ToLot;
            originReceiptDetail.LpnID = receiptDetailInfo.LpnID;
            originReceiptDetail.ToLpnID = receiptDetailInfo.ToLpnID;
            originReceiptDetail.UpdatedBy = pubParasInfo.UserID;
            if (receiptInfo.IsPalletized == "Y")
            {
                receiptDetailLpnDA.UpdateReceiptDetailStatus4Receiving(dataBase, tran, originReceiptDetail, whLoginID);
            }
            else
            {
                receiptDetailDA.UpdateReceiptDetailStatus4Receiving(dataBase, tran, originReceiptDetail, whLoginID);
            }
        }

        public void UpdateReceiptQtyStatus(DataBase dataBase, DbTransaction tran, ReceiptInfo receiptInfo, string whLoginID, PubParasInfo pubParasInfo)
        {
            string status = this.GetASNStatus(dataBase, tran, receiptInfo, whLoginID, pubParasInfo);
            receiptDA.UpdateReceiptStatusAndDate(dataBase, tran, receiptInfo.ReceiptID, receiptInfo.ReceiptDate, status, whLoginID, pubParasInfo.UserID);
        }

        public void UpdateTransStatus(DataBase dataBase, DbTransaction tran, string TransID, string updateBy, string transStatus, string whLoginID)
        {
            invTranDA.UpdateTransStatus(dataBase, tran, TransID, updateBy, transStatus, whLoginID);
        }
    }
}

